<template>
    <div>
        <div class="search-term">
            <el-form :inline="true" :model="searchInfo" class="demo-form-inline" ref="queryForm">
                <el-form-item label="集群名称" >
                    <el-select v-model="searchInfo.cluster_name" clearable  disabled placeholder="请选择">
                        <el-option
                                v-for="(dict,index) in databaseOptions"
                                :key="index"
                                :label="dict.cluster_name"
                                :value="dict.cluster_name"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="账号名称" prop="user_name" >
                    <el-input placeholder="搜索条件" v-model="searchInfo.user_name" clearable></el-input>
                </el-form-item>
                <el-form-item label="启用状态" prop="status">
                    <el-select v-model="searchInfo.status" placeholder="请选择" clearable>
                        <el-option key="true" label="正常" value="true"></el-option>
                        <el-option key="false" label="关闭" value="false"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button @click="handleQuery" icon="el-icon-search" type="primary">查询</el-button>
                </el-form-item>
            </el-form>
            <el-form>
                <el-form-item>
                    <el-button @click="createDatabaseUser()" type="primary">新增账户</el-button>
                </el-form-item>
            </el-form>
        </div>
        <el-table
                :data="tableData"
                border
                ref="multipleTable"
                stripe
                style="width: 100%"
                tooltip-effect="dark"
        >
            <el-table-column label="ID" prop="ID" ></el-table-column>
            <el-table-column label="集群名称" prop="cluster_name" ></el-table-column>
            <el-table-column label="数据库名称" prop="database_name" ></el-table-column>
            <el-table-column label="账户名称" prop="user_name" ></el-table-column>
            <el-table-column prop="project.project_fullname" label="项目名称"></el-table-column>
<!--            <el-table-column label="账户密码" prop="password" ></el-table-column>-->
            <el-table-column label="操作权限" prop="permission" >
                <template slot-scope="scope">{{scope.row.permission | basecode(scope.row.permission,'database_operation_auth')}}</template>
            </el-table-column>
            <el-table-column label="启用状态" prop="status" >
                <template slot-scope="scope">{{scope.row.status | basecode(scope.row.status,'sys_common_status')}}</template>
            </el-table-column>
<!--            <el-table-column label="公钥" prop="public_key" ></el-table-column>-->
<!--            <el-table-column label="私钥" prop="public_key" ></el-table-column>-->
<!--            <el-table-column label="加密密码" prop="public_key" ></el-table-column>-->
            <el-table-column label="操作" width="200">
                <template slot-scope="scope">
                    <el-button @click="updateDatabaseUser(scope.row)" icon="el-icon-edit" size="small" type="primary">编辑</el-button>
                    <el-button @click="deleteDatabaseUser(scope.row)" type="danger" icon="el-icon-delete" style="margin-left: 6px;" size="mini" slot="reference">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <el-pagination
                :current-page="page"
                :page-size="pageSize"
                :page-sizes="[10, 30, 50, 100]"
                :style="{float:'right',padding:'20px'}"
                :total="total"
                @current-change="handleCurrentChange"
                @size-change="handleSizeChange"
                layout="total, sizes, prev, pager, next, jumper"
        ></el-pagination>

        <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" width="600px" >
            <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="110px">
                <el-row :gutter="10">
                    <el-col :span="20" :offset="2">
                    <el-col :span="24">
                            <el-form-item label="集群名称" prop="cluster_name">
                                <el-input
                                        v-model="formData.cluster_name"
                                        disabled
                                        placeholder="请输入集群名称"
                                        clearable
                                ></el-input>
                            </el-form-item>
                        </el-col>
                    <el-col :span="24">
                        <el-form-item label="数据库名称" prop="database_name">
                            <el-input
                                    v-model="formData.database_name"
                                    placeholder="请输入数据库名称"
                                    clearable
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="启用状态" prop="status" required>
                            <el-switch v-model="formData.status" active-text="开启" inactive-text="停用"></el-switch>
                        </el-form-item>
                     </el-col>
                     <el-col :span="24">
                            <el-form-item label="项目"  prop="project_code">
                                <el-select  placeholder="请选择项目"  v-model="formData.project_code" >
                                    <el-option
                                            v-for="(item, i) in getProjectOptions"
                                            :key="i"
                                            :label="item.project_fullname"
                                            :value="item.project_code"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                     </el-col>
                     <el-col :span="24">
                        <el-form-item label="用户权限" prop="permission" style="width:100%" >
                            <el-radio-group v-model="formData.permission" >
                                <el-radio
                                    v-for="dict in permissionOptions"
                                    :key="dict.item_name"
                                    :label="dict.item_value"
                                    >{{ dict.item_name }}</el-radio>
                            </el-radio-group>
                        </el-form-item>
                     </el-col>
                    <el-col :span="24">
                        <el-form-item label="账户名称" prop="user_name">
                            <el-input
                                    v-model="formData.user_name"
                                    placeholder="请输入账户名称"
                                    clearable
                            ></el-input>
                        </el-form-item>
                    </el-col>
<!--                    <el-col :span="24">-->
<!--                        <el-form-item label="加密工具" prop="encryption_tools" style="width:100%" >-->
<!--                            <el-radio-group v-model="formData.encryption_tools">-->
<!--                                <el-radio-->
<!--                                        v-for="dict in encryptionToolsOptions"-->
<!--                                        :key="dict.item_name"-->
<!--                                        :label="dict.item_value"-->
<!--                                >{{ dict.item_name }}</el-radio>-->
<!--                            </el-radio-group>-->
<!--                        </el-form-item>-->
<!--                    </el-col>-->
                    <el-col :span="24">
                        <el-form-item label="账户密码" prop="password">
                            <el-input
                                    v-model="formData.password"
                                    placeholder="请输入账户密码"
                                    clearable
                            ></el-input>
                        </el-form-item>
                    </el-col>
<!--                    <el-col :span="4">-->
<!--                        <el-form-item label="" label-width="0">-->
<!--                            <el-button class="left-mg-xs" type="primary" size="medium" @click="Encryption()">加密</el-button>-->
<!--                        </el-form-item>-->
<!--                    </el-col>-->


<!--                    <el-col :span="24">-->
<!--                        <el-form-item label="加密密码" prop="encryption_password">-->
<!--                            <el-input v-model="formData.encryption_password" placeholder="请输入公钥" clearable type="textarea"></el-input>-->
<!--                        </el-form-item>-->
<!--                    </el-col>-->
<!--                    <el-col :span="24">-->
<!--                        <el-form-item label="公钥" prop="public_key">-->
<!--                            <el-input v-model="formData.public_key" placeholder="请输入公钥" clearable type="textarea"></el-input>-->
<!--                        </el-form-item>-->
<!--                    </el-col>-->
<!--                    <el-col :span="24">-->
<!--                        <el-form-item label="私钥" prop="private_key">-->
<!--                            <el-input v-model="formData.private_key" placeholder="请输入公钥" clearable type="textarea"></el-input>-->
<!--                        </el-form-item>-->
<!--                    </el-col>-->
                    </el-col>
                </el-row>
            </el-form>
            <div class="dialog-footer" slot="footer">
                <el-button @click="closeDialog">取 消</el-button>
                <el-button @click="enterDialog" type="primary">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {
        getDatabaseUserList,
        deleteDatabaseUser,
        updateDatabaseUser,
        createDatabaseUser,
    } from "@/api/cmdb_databaseUser"; //  此处请自行替换地址
    import { getDatabaseList } from "@/api/cmdb_database";
    import { getProjectList  }  from "@/api/cmdb_project";
    import infoList from "@/mixins/infoList";

    export default {
        name: "databaseUsers",
        mixins: [infoList],
        data() {
            return {
                databaseOptions: [],
                getProjectOptions:[],
                permissionOptions:[],
                encryptionToolsOptions:[],
                listApi: getDatabaseUserList,
                dialogFormVisible: false,
                dialogTitle: "新增账户",
                visible: false,
                type: "",
                searchInfo: {
                    cluster_name: undefined,
                    user_name: undefined,
                    status: undefined
                },
                formData: {
                    cluster_name:'',
                    database_name: '',
                    user_name: '',
                    // encryption_tools:'Druid',
                    password: '',
                    status: true,
                    permission: 'WR',
                    project_code:'',
                    // encryption_password: null,
                    // public_key: null,
                    // private_key: null
                },
                rules: {
                    database_name: [
                        {
                            required: true,
                            message: "请输入数据库名称",
                            trigger: "blur"
                        }
                    ],
                    project_code: [
                        {
                            required: true,
                            message: "请选择项目",
                            trigger: "blur"
                        }
                    ],
                    user_name: [
                        {
                            required: true,
                            message: "请输入账户名称",
                            trigger: "blur"
                        }
                    ],
                    password: [
                        {
                            required: true,
                            message: "请输入账户密码",
                            trigger: "blur"
                        }
                    ]
                }
            };
        },
        methods: {
            //条件搜索前端看此方法
            handleQuery() {
                this.searchInfo.cluster_name = this.$route.params.id
                this.page =1
                this.pageSize=10
                if (this.searchInfo.status == "") {
                    this.searchInfo.status = null;
                }
                this.getTableData();
            },
            Encryption(){},
            async updateDatabaseUser(row) {
                this.dialogTitle = '编辑账户'
                this.type = "update";
                this.formData = row
                this.dialogFormVisible = true;
                this.permissionOptions = this.getDicItemList('database_operation_auth')
                this.encryptionToolsOptions = this.getDicItemList('encryption_tools')
            },
            closeDialog() {
                this.dialogFormVisible = false;
                this.formData = {
                    cluster_name: this.$route.params.id,
                    database_name:'',
                    user_name: '',
                    password: '',
                    status: true,
                    permission: 'WR',
                    project_code:'',
                };
            },
            deleteDatabaseUser(row) {
                this.$confirm("此操作将永久删除该字典项, 是否继续?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                })
                    .then(async () => {
                        this.visible = false;
                        const res = await deleteDatabaseUser({ ID: row.ID });
                        if (res.code == 0) {
                            this.$message({
                                type: "success",
                                message: "删除成功!"
                            });
                            await this.getTableData();
                        }
                    })
                    .catch(() => {
                        this.$message({
                            type: "info",
                            message: "已取消删除"
                        });
                    });
            },
            async enterDialog() {
                this.formData.sysDictionaryID = Number(this.$route.params.id)
                this.$refs['elForm'].validate(async valid => {
                    if (!valid) return
                    let res;
                    switch (this.type) {
                        case "create":
                            res = await createDatabaseUser(this.formData);
                            break;
                        case "update":
                            res = await updateDatabaseUser(this.formData);
                            break;
                        default:
                            res = await createDatabaseUser(this.formData);
                            break;
                    }
                    if (res.code == 0) {
                        this.$message({
                            type:"success",
                            message:"创建/更改成功"
                        })
                        this.closeDialog();
                        await this.getTableData();
                    }
                })

            },
            async createDatabaseUser(){
                this.dialogTitle = '新增账户'
                this.type = 'create';
                this.dialogFormVisible = true;
                this.permissionOptions = this.getDicItemList('database_operation_auth')
                this.encryptionToolsOptions = this.getDicItemList('encryption_tools')
                this.formData = {
                    cluster_name: this.$route.params.id,
                    database_name:'',
                    user_name: '',
                    password: '',
                    status: true,
                    permission: 'WR',
                    project_code:'',
                };
            }
        },
        async created() {
            this.searchInfo.cluster_name = this.$route.params.id
            await this.getTableData();
            this.formData.cluster_name = this.$route.params.id
            this.statusOptions = this.getDicItemList('sys_common_status')
            getDatabaseList({page:1,pageSize:99}).then(response => {
                this.databaseOptions = response.data.list;
            })
            getProjectList({'page':1,'pageSize': 100}).then(response => {
                this.getProjectOptions = response.data.list;
            })
        }
    };
</script>

<style type="text/css" scoped="scoped">
    .link-type, .link-type:focus {
        color: #337ab7;
        cursor: pointer;
    }
</style>

